package ${package.Controller};


import com.file.system.entity.res.CommonResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.file.system.tools.regular.RegularUtil;

import org.springframework.web.bind.annotation.PostMapping;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.file.system.entity.res.DataPage;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import static cn.hutool.core.util.ObjectUtil.isNotEmpty;
import static cn.hutool.core.util.ObjectUtil.isNotNull;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};

import org.springframework.web.bind.annotation.RestController;
import java.util.List;


/**
* ${cfg.controllerName}
*
* @author ${author}
*/
@RestController
@Validated
@RequestMapping("${cfg.controllerPath}")
public class ${table.controllerName} {

    @Autowired
    private ${table.serviceName} service;

    /**
    * 查询${cfg.controllerName}分页
    *
    * @param pageIndex 页数
    * @param pageSize  每页条数
    * @return CommonResult
    * @apiNote -
    * @author ${author}
    */
    @PostMapping("/page/{pageIndex}/{pageSize}")
    public CommonResult<DataPage<List<${entity}>>> page(@PathVariable
                                                          @Min(value = 1, message = "页数最小1")
                                                          Integer pageIndex,
                                                          @PathVariable
                                                          @Min(value = 1, message = "每页条数最小1")
                                                          @Max(value = 100, message = "每页条数最大100")
                                                          Integer pageSize,
                                                          @RequestBody @Validated ${entity} ${entity?uncap_first}
        ) {
        QueryWrapper<${entity}> query = new QueryWrapper<>();
<#if table.fieldNames?contains("sort")>
        query.orderByAsc("a.sort");
<#else>
        query.orderByDesc("a.id");
</#if>
        query.eq("a.is_del", 0);
<#list table.fields as field>
<#if field.name != "sort" && field.name != "id">
    <#if  field.customMap.data_type == "int">
        query.eq(ObjectUtil.isNotNull(${entity?uncap_first}.get${field.propertyName?cap_first}()),"a.${field.name}", ${entity?uncap_first}.get${field.propertyName?cap_first}());
    <#elseif field.customMap.data_type == "bigint">
        query.eq(ObjectUtil.isNotNull(${entity?uncap_first}.get${field.propertyName?cap_first}()),"a.${field.name}", ${entity?uncap_first}.get${field.propertyName?cap_first}());
    <#else>
        query.like(StrUtil.isNotEmpty(${entity?uncap_first}.get${field.propertyName?cap_first}()),"a.${field.name}", ${entity?uncap_first}.get${field.propertyName?cap_first}());
    </#if>
</#if>
</#list>
        //查询集合
        DataPage<List<${entity}>> dp = service.page(pageIndex, pageSize, query);
        return CommonResult.ok().setResult(dp);
    }

    /**
    * 通过ids查询${cfg.controllerName}集合
    *
    * @return CommonResult
    * @apiNote -
    * @author ${author}
    */
    @PostMapping("/listByIds")
    public CommonResult<List<${entity}>> listByIds(@RequestBody List<Long> ids) {
        //查询集合
        List<${entity}> list = service.listByIds(ids);
        return CommonResult.ok().setResult(list);
    }

    /**
    * 查询所有非删除状态${cfg.controllerName}
    *
    * @return CommonResult
    * @apiNote -
    * @author ${author}
    */
    @PostMapping("/listAll")
    public CommonResult<List<${entity}>> listAll() {
        //查询集合
        List<${entity}> list = service.listAll();
        return  CommonResult.ok().setResult(list);
    }


    /**
    * 通过id查询${cfg.controllerName}
    *
    * @param id ${cfg.controllerName}id
    * @return CommonResult
    * @apiNote -
    * @author ${author}
    */
    @PostMapping("/id/{id:" + RegularUtil.ID + "}")
    public CommonResult<${entity}> getById(@PathVariable
                                              @Min(value = 1, message = "id长度最小1")
                                              Long id) {
        //查询集合
        ${entity} ${entity?uncap_first} = service.getById(id);
        return CommonResult.ok().setResult(${entity?uncap_first});
    }

    /**
    * 通过id删除${cfg.controllerName}
    *
    * @param id id
    * @return CommonResult
    * @apiNote -
    * @author ${author}
    */
    @PostMapping("/remove/{id:" + RegularUtil.ID + "}")
    public CommonResult removeById(@PathVariable
                            @Min(value = 1, message = "id长度最小1")
                            Long id) {
        //返回
        return CommonResult.check(service.removeById(id));
    }

    /**
    * 通过ids删除${cfg.controllerName}
    *
    * @return CommonResult
    * @apiNote -
    * @author ${author}
    */
    @PostMapping("/removeByIds")
    public CommonResult removeById(@RequestBody List<Long> ids) {
        //返回
        return CommonResult.check(service.removeByIds(ids));
    }


    /**
    * 通过id编辑${cfg.controllerName}
    *
    * @param ${entity?uncap_first} ${cfg.controllerName}
    * @return CommonResult
    * @apiNote -
    * @author ${author}
    */
    @PostMapping("/updateById")
    public CommonResult<${entity}> updateById(@RequestBody @Validated(${entity}.update.class) ${entity} ${entity?uncap_first}) {
        //编辑修改
        return CommonResult.check(service.updateById(${entity?uncap_first})).setResult(${entity?uncap_first});
    }


    /**
    * 添加${cfg.controllerName}
    *
    * @param ${entity?uncap_first} ${cfg.controllerName}
    * @return CommonResult
    * @apiNote -
    * @author ${author}
    */
    @PostMapping("/save")
    public CommonResult<${entity}> save(@RequestBody @Validated(${entity}.add.class) ${entity} ${entity?uncap_first}) {
        //添加${cfg.controllerName}
        return CommonResult.check(service.save(${entity?uncap_first})).setResult(${entity?uncap_first});
    }
}

